using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Configuration;
using EnvDTE;
using Microsoft.Practices.RecipeFramework.Extensions.Converters;
using Microsoft.Practices.WizardFramework;

namespace Microsoft.Practices.RepositoryFactory.CustomPages
{
    /// <summary/>
    public partial class ConnectionCP : CustomWizardPage
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionCP"/> class.
        /// </summary>
        /// <param name="parent">The parent.</param>
        public ConnectionCP(WizardForm parent)
            : base(parent)
        {
            InitializeComponent();
        }

        #endregion

        #region Arguments

        /// <summary>
        /// Sets the configuration item.
        /// </summary>
        /// <value>The configuration item.</value>
        [RecipeArgument]
        public ProjectItem ConfigurationItem
        {
            set
            {
                this.veHostProject.Value = value;
            }
            get { return this.veHostProject.Value as ProjectItem; }
        }

        /// <summary>
        /// Sets the connection settings.
        /// </summary>
        /// <value>The connection settings.</value>
        [RecipeArgument]
        public ConnectionStringSettings ConnectionSettings
        {
            set
            {
                this.veConnectionSettings.Value = value;
            }
            get { return this.veConnectionSettings.Value as ConnectionStringSettings; }
        }

        #endregion

        /// <summary>
        /// Gets or sets the site of the control.
        /// </summary>
        /// <value></value>
        /// <returns>The <see cref="System.ComponentModel.ISite"></see> associated with the <see cref="System.Windows.Forms.Control"></see>, if any.</returns>
        /// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
        public override ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                base.Site = value;
                if(value != null)
                {
                    this.veHostProject.BeginInit();
                    this.veHostProject.ValueType = typeof(Project);
                    base.Site.Container.Add(veHostProject);
                    ProjectWithResponsibilityConverter converter1 = new ProjectWithResponsibilityConverter();                    
                    StringDictionary attributesConverter1 = new StringDictionary();
                    attributesConverter1.Add("ResponsibilityArgument", "IsHostProject");
                    converter1.Configure(attributesConverter1);
                    this.veHostProject.ConverterInstance = converter1;
                    this.veHostProject.EndInit();

                    this.veConnectionSettings.BeginInit();
                    this.veConnectionSettings.ValueType = typeof(ConnectionStringSettings);
                    base.Site.Container.Add(veConnectionSettings);
                    ConnectionStringConverter converter2 = new ConnectionStringConverter();
                    StringDictionary attributesConverter2 = new StringDictionary();
                    attributesConverter2.Add("ConfigurationItemArgument", "ConfigurationItem");
                    converter2.Configure(attributesConverter2);
                    this.veConnectionSettings.ConverterInstance = converter2;
                    this.veConnectionSettings.EndInit();
                }
            }
        }

        private void veConnectionSettings_ValueChanged(object sender, ComponentChangedEventArgs e)
        {
            ConnectionStringSettings settings = (ConnectionStringSettings)veConnectionSettings.Value;
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            dictservice.SetValue("ConnectionSettings", settings);
        }

        private void veHostProject_ValueChanged(object sender, ComponentChangedEventArgs e)
        {
            Project project = (Project)veHostProject.Value;
            IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
            dictservice.SetValue("ServiceHostProject", project);
        }
    }
}